package com.ujs.order.controller;


import com.ujs.commonutils.constant.R;
import com.ujs.order.service.TOrderService;
import com.ujs.order.service.TPayLogService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-09-03
 */
@RestController
@RequestMapping("/eduorder/tpaylog")
public class TPayLogController {
    @Autowired
    private TPayLogService payLogService;

    @ApiOperation("生成微信支付二维码")
    @GetMapping("/createNative/{orderSn}")
    public R createNative(@PathVariable("orderSn") String orderSn) {
        // 返回信息，包含二维码地址，还有其它信息
        Map<String, Object> map = payLogService.createNative(orderSn);
        return R.ok().data(map);
    }

    @ApiOperation("查询订单支付状态")
    @GetMapping("/queryOrderPayStatus/{orderSn}")
    public R queryOrderPayStatus(@PathVariable("orderSn") String orderSn) {
        // 返回信息，包含二维码地址，还有其它信息
        Map<String, String> map = payLogService.queryOrderPayStatus(orderSn);
        if (map == null) {
            return R.error().message("支付出错了");
        }
        if (map.get("trade_state").equals("SUCCESS")) {
            // 支付成功，添加记录到支付表，更新订单表状态
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }
        return R.ok().code(25000).message("支付中");
    }
}

